package com.ihelp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ihelp.pojo.*;
import com.ihelp.mapper.RequestMapper;
import com.ihelp.service.RequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihelp.util.MyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-05-23
 */
@Service
public class RequestServiceImpl extends ServiceImpl<RequestMapper, Request> implements RequestService {

    @Autowired
    private RequestMapper requestMapper;

    @Override
    public List<RequestDTO> getAllRequest(int pageId) {
        IPage<RequestDTO> requestPage = requestMapper.selectJoinPage(
                new Page<>(pageId, 10),
                RequestDTO.class,
                MyWrapper.allRequestWrapper());
        List<RequestDTO> requestByPage = requestPage.getRecords();
        return requestByPage;
    }

    @Override
    public List<RequestDTO> getAllRequestByDynamicConditions(
            Integer pageId,
            Integer hospitalId,
            Integer hospitalDepartmentId,
            String visitTime,
            Integer visitorId,
            Double price,
            Integer serviceTypeId,
            Integer areaId,
            Integer duration) {
        System.out.println(price);

        MPJLambdaWrapper<Request> requestMPJLambdaWrapper = MyWrapper.allRequestWrapper();

        if (hospitalId != null && hospitalId > 0) {
            requestMPJLambdaWrapper.eq("hospital_id", hospitalId);
        }
        if (hospitalDepartmentId != null && hospitalDepartmentId > 0) {
            requestMPJLambdaWrapper.eq("hospital_department_id", hospitalDepartmentId);
        }
        if (visitTime != null) {
            requestMPJLambdaWrapper.eq("visit_time", visitTime);
        }
        if (visitorId != null && visitorId > 0) {
            requestMPJLambdaWrapper.eq("visitor_id", visitorId);
        }
        if (hospitalId != null && hospitalId > 0) {
            requestMPJLambdaWrapper.eq("hospital_id", hospitalId);
        }
        if (price != null && price >= 0) {
            requestMPJLambdaWrapper.eq("price", price);
        }
        if (serviceTypeId != null && serviceTypeId > 0) {
            requestMPJLambdaWrapper.eq("service_type_id", serviceTypeId);
        }
        if (areaId != null && areaId > 0) {
            requestMPJLambdaWrapper.eq("area_id", areaId);
        }
        if (duration != null && duration > 0) {
            requestMPJLambdaWrapper.eq("duration", duration);
        }


        IPage<RequestDTO> requestPage = requestMapper.selectJoinPage(
                new Page<>(pageId, 10),
                RequestDTO.class,
                requestMPJLambdaWrapper
        );
        List<RequestDTO> requestByPage = requestPage.getRecords();
        return requestByPage;
    }

    @Override
    public Integer deleteRequestById(Integer id) {
        int result = requestMapper.deleteById(id);
        return result;
    }

    @Override
    public Integer createRequest(Integer hospitalId,
                                 Integer hospitalDepartmentId,
                                 String visitTime,
                                 Double price,
                                 String describes,
                                 Integer serviceTypeId,
                                 Integer visitorId,
                                 Integer areaId,
                                 Double duration
    ) {
        Request request = new Request();
        request.setVisitorId(visitorId);
        request.setHospitalId(hospitalId);
        request.setHospitalDepartmentId(hospitalDepartmentId);
        request.setVisitTime(visitTime);
        request.setPrice(price);
        request.setDescribes(describes);
        request.setServiceTypeId(serviceTypeId);
        request.setAreaId(areaId);
        request.setDuration(duration);
        int result = requestMapper.insert(request);
        return result;
    }

    @Override
    public RequestDTO getRequestById(Integer id) {
        MPJLambdaWrapper<Request> requestMPJLambdaWrapper = MyWrapper.allRequestWrapper();
        return requestMapper.selectJoinOne(RequestDTO.class, requestMPJLambdaWrapper.eq("t.id", id));
    }

}
